package com.dap.cms.governance.controller;

import com.dap.dao.model.QueryResult;
import com.dap.cms.governance.po.CmsComponentManagerPo;
import com.dap.cms.governance.service.CmsComponentManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fesine.commons.entity.Result;
import com.fesine.commons.enums.ResultEnum;
import com.fesine.commons.util.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: t_cms_component_manager表对应的控制层类
 * @author: Fesine
 * @createTime:2018/08/21
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2018/08/21
 */
@RestController
@RequestMapping("/v1")
public class CmsComponentManagerController {
    private static final Logger logger=LoggerFactory.getLogger(CmsComponentManagerController.class);
    @Autowired
    private CmsComponentManagerService cmsComponentManagerService;

    /**
     * 新增CmsComponentManagerPo
     * @param cmsComponentManagerPo
     * @return result
     */
    @PostMapping("/cmsComponentManager")
    public Result addCmsComponentManager(CmsComponentManagerPo cmsComponentManagerPo){
        try{
            cmsComponentManagerService.save(cmsComponentManagerPo);
            return ResultUtils.success(ResultEnum.CREATED,null);
        }catch(Exception e){
            logger.error("新增失败",e);
            return ResultUtils.error(500,"新增失败");
        }

    }

    /**
     * 更新CmsComponentManagerPo
     * @param cmsComponentManagerPo
     * @return result
     */
    @PutMapping("/cmsComponentManager")
    public Result updateCmsComponentManager(CmsComponentManagerPo cmsComponentManagerPo){
        try{
            cmsComponentManagerService.update(cmsComponentManagerPo);
            return ResultUtils.success(ResultEnum.CREATED,null);
        }catch(Exception e){
            logger.error("更新失败",e);
            return ResultUtils.error(500,"更新失败");
        }

    }

    /**
     * 删除CmsComponentManagerPo
     * @param id
     * @return result
     */
    @DeleteMapping("/cmsComponentManager/{id}")
    public Result deleteCmsComponentManager(@PathVariable("id") Integer id){
        CmsComponentManagerPo cmsComponentManagerPo=new CmsComponentManagerPo();
        cmsComponentManagerPo.setId(id);
        cmsComponentManagerService.delete(cmsComponentManagerPo);
        return ResultUtils.success(cmsComponentManagerPo);
    }

    /**
     * 查看CmsComponentManagerPo详情
     * @param id
     * @return result
     */
    @GetMapping("/cmsComponentManager/{id}")
    public Result getCmsComponentManager(@PathVariable("id") Integer id){
        CmsComponentManagerPo cmsComponentManagerPo=new CmsComponentManagerPo();
        cmsComponentManagerPo.setId(id);
        cmsComponentManagerPo=cmsComponentManagerService.get(cmsComponentManagerPo);
        return ResultUtils.success(cmsComponentManagerPo);
    }

    /**
     * 查询CmsComponentManagerPo列表
     * @param cmsComponentManagerPo
     * @return result
     */
    @GetMapping("/cmsComponentManagers")
    public Result listAll(CmsComponentManagerPo cmsComponentManagerPo){
        List<CmsComponentManagerPo> list=cmsComponentManagerService.listAll(cmsComponentManagerPo);
        return ResultUtils.success(list);
    }

    /**
     * 分页查询CmsComponentManagerPo列表
     * @param cmsComponentManagerPo
     * @param pageIndex
     * @param pageSize
     * @return result
     */
    @GetMapping("/cmsComponentManagers/page")
    public Result listPage(CmsComponentManagerPo cmsComponentManagerPo,@RequestParam(value = "pageIndex",
        required = false, defaultValue = "1")
            Integer pageIndex,@RequestParam(value = "pageSize", required = false, defaultValue =
        "10") Integer pageSize){
        QueryResult<CmsComponentManagerPo> result=cmsComponentManagerService.listPage(cmsComponentManagerPo,pageIndex,pageSize);
        long total=result.getTotalrecord();
        Map<String, Object> map=new HashMap(16);
        if(total > 0){
            map.put("totalRecord",total);
            map.put("resultList",result.getResultlist());
        }
        return ResultUtils.success(map);
    }

}

